﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DLL;
using PagedList;
using CommonHelpers;

namespace QuanLyBanLe
{
    public partial class FormInProductHistory : Form
    {
        private InProductService m_InProductService;
        private int m_PageNumber;
        private int m_PageSize;
        private int m_MaxPage;
        private bool m_EnabledEvent;
        private int m_TotalRecord;
        private bool isFiltered = false;
        private int m_RowNumber;
        private IList<pInProdPaper_Get_Result> m_InProductHistory;

        public FormInProductHistory()
        {
            m_InProductService = new InProductService();
            m_PageNumber = 1;
            m_PageSize = 20;
            m_EnabledEvent = false;
            InitializeComponent();
            dtgInProdPaper.AutoGenerateColumns = false;
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            m_PageNumber = 1;
            ChangePageInProdPaper();
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            m_PageNumber = m_PageNumber > 1 ? m_PageNumber - 1 : m_PageNumber;
            ChangePageInProdPaper();
        }

        private void mnrPageIndex_ValueChanged(object sender, EventArgs e)
        {
            if (m_EnabledEvent)
            {
                m_PageNumber = Convert.ToInt32(mnrPageIndex.Value);
                ChangePageInProdPaper();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            m_PageNumber = m_PageNumber < m_MaxPage ? m_PageNumber + 1 : m_PageNumber;
            ChangePageInProdPaper();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            m_PageNumber = m_MaxPage;
            ChangePageInProdPaper();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            m_EnabledEvent = false;
            isFiltered = true;
            m_PageNumber = 1;
            m_InProductHistory = m_InProductService.GetInProdPaper(dtpFromDate.Value.Date, dtpToDate.Value.Date);
            BindPaperInfoToGrid();
            CalculateTotalMoney();
        }

        private void SetEnabledForPagingNavigator()
        {
            if (m_PageNumber == m_MaxPage)
            {
                if (m_PageNumber == 1)
                {
                    btnFirst.Enabled = false;
                    btnPrev.Enabled = false;
                    btnLast.Enabled = false;
                    btnNext.Enabled = false;
                }
                else
                {
                    btnFirst.Enabled = true;
                    btnPrev.Enabled = true;
                    btnLast.Enabled = false;
                    btnNext.Enabled = false;
                }
            }
            else if (m_PageNumber == 1)
            {
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                btnLast.Enabled = true;
                btnNext.Enabled = true;
            }
            else
            {
                btnFirst.Enabled = true;
                btnPrev.Enabled = true;
                btnLast.Enabled = true;
                btnNext.Enabled = true;
            }
        }

        private void FormInProductHistory_Load(object sender, EventArgs e)
        {
            lbTotalMoney.Text = "";
            m_InProductHistory = m_InProductService.GetInProdPaper(DateTime.Now, DateTime.Now);
            BindPaperInfoToGrid();
            CalculateTotalMoney();
            this.Text += FormMain.GetShopInfo();
        }

        private void ChangePageInProdPaper()
        {
            BindPaperInfoToGrid();
        }

        private void BindPaperInfoToGrid()
        {
            m_EnabledEvent = false;
            var inProductDetails = m_InProductHistory.ToPagedList(m_PageNumber, m_PageSize);
            dtgInProdPaper.DataSource = inProductDetails.ToList();
            m_TotalRecord = inProductDetails.TotalItemCount;
            m_MaxPage = inProductDetails.PageCount;
            if (m_TotalRecord > 0)
            {
                lblTotalPage.Text = "/" + m_MaxPage.ToString();
                mnrPageIndex.Minimum = 1;
                mnrPageIndex.Enabled = true;
                mnrPageIndex.Maximum = m_MaxPage;
                mnrPageIndex.Value = m_PageNumber;
                SetEnabledForPagingNavigator();
            }
            else
            {
                m_PageNumber = 0;
                mnrPageIndex.Minimum = 0;
                mnrPageIndex.Value = 0;
                mnrPageIndex.Enabled = false;
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                btnLast.Enabled = false;
                btnNext.Enabled = false;
                lblTotalPage.Text = string.Empty;
            }
            for (int i = 0; i < dtgInProdPaper.Rows.Count; i++)
            {
                dtgInProdPaper["colRowNumber", i].Value = (m_PageNumber - 1) * m_PageSize + i + 1;
            }
            m_EnabledEvent = true;
        }

        private void dtgInProdPaper_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dtgInProdPaper.Columns["colViewDetail"].Index && e.RowIndex != -1)
            {
                var paperId = Convert.ToInt32(dtgInProdPaper["colId", e.RowIndex].Value.ToString());
                var paper = m_InProductHistory.Where(x => x.Id == paperId).FirstOrDefault();
                FormInProdPaper form = new FormInProdPaper(paperId,paper.Date);
                form.ShowDialog();
            }

            if (e.ColumnIndex == dtgInProdPaper.Columns["colUpdate"].Index && e.RowIndex != -1)
            {
                var paperId = Convert.ToInt32(dtgInProdPaper["colId", e.RowIndex].Value.ToString());
                var paper = m_InProductHistory.Where(x => x.Id == paperId).FirstOrDefault();
                FormInProdPaper form = new FormInProdPaper(paperId, paper.Date, true);
                form.ShowDialog();
                ReloadData();
                CalculateTotalMoney();
            }
        }

        public void ReloadData()
        {
            if (isFiltered)
            {
                m_InProductHistory = m_InProductService.GetInProdPaper(dtpFromDate.Value.Date, dtpToDate.Value.Date);
            }
            else
            {
                m_InProductHistory = m_InProductService.GetInProdPaper(DateTime.Now, DateTime.Now);
            }
            ChangePageInProdPaper();
        }

        public void CalculateTotalMoney()
        {
            long totalMoney = 0;
            long totalAmount = 0;
            if (m_InProductHistory.Count > 0)
            {
                foreach (var history in m_InProductHistory)
                {
                    if (history.TotalAmount.HasValue)
                    {
                        totalAmount += history.TotalAmount.Value;
                    }
                    if (history.TotalMoney.HasValue)
                    {
                        totalMoney += history.TotalMoney.Value;
                    }
                }
            }
            lbTotalMoney.Text = totalMoney.ToMoney();
        }
    }
}
